# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_realname=nspr
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.35
pkgrel=2
pkgdesc="Netscape Portable Runtime (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSPR"
msys2_references=(
  "cpe: cpe:/a:mozilla:netscape_portable_runtime"
)
license=('spdx:MPL-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "nsinstall>=${pkgver}"
             "zip")
options=('!emptydirs')
source=(https://ftp.mozilla.org/pub/mozilla.org/nspr/releases/v${pkgver}/src/${_realname}-${pkgver}.tar.gz
        0001-fix-integer-casting.patch
        nspr-4.10.2_autotools.patch
        nspr-4.10.2_mingw.patch
        nspr-4.10.2_staticbuild.patch
        nspr-4.10.2_x64.patch
        nspr-4.34_aarch64.patch)
sha256sums=('7ea3297ea5969b5d25a5dd8d47f2443cda88e9ee746301f6e1e1426f8a6abc8f'
            'f41e9ef3f2147512ae1e3fb9ea1df634fa089dffa3028b908ac7a08f95e370d1'
            '3108ea02a33dbe8d612b70c44b60826d8beea1bbfa907668dc798f8061fb4ad4'
            '4b57ba28c6d1384ee5f26fcc2f2f51f81b6b2bfd0e89c1c8da4ddd26b8fffc93'
            'bdb7138b12b7fbc32e906347e0dc963342316032ac3db8586683e54e40126e26'
            '599ab9edb21568c02c4e6e96b366e51c042a9e789b72b9e7d9b7acc283757ab4'
            '3acaefda8dbccf1c1239d08264b0e191b01dc6f0de2e8c1262ddb96fbf288656')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}/nspr
  patch -p1 -i "${srcdir}"/0001-fix-integer-casting.patch
  patch -p0 -i "${srcdir}"/nspr-4.10.2_autotools.patch
  patch -p0 -i "${srcdir}"/nspr-4.10.2_mingw.patch
  patch -p0 -i "${srcdir}"/nspr-4.10.2_staticbuild.patch
  patch -p0 -i "${srcdir}"/nspr-4.10.2_x64.patch
  patch -p1 -i "${srcdir}"/nspr-4.34_aarch64.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  if [ "$CARCH" = "x86_64" ]; then
    _confflags="--enable-64bit"
  else
    _confflags=""
  fi

  "${srcdir}"/${_realname}-${pkgver}/nspr/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --includedir=${MINGW_PREFIX}/include/nspr \
    --enable-optimize \
    --disable-debug \
    ${_confflags}

  make
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make DESTDIR="${pkgdir}" install

  mv "${pkgdir}"${MINGW_PREFIX}/lib/*.dll "${pkgdir}"${MINGW_PREFIX}/bin/

  ln -s nspr.pc "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/mozilla-nspr.pc"
  rm -r "${pkgdir}"${MINGW_PREFIX}/bin/{compile-et.pl,prerr.properties} \
        "${pkgdir}"${MINGW_PREFIX}/include/nspr/md

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/nspr/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
